<template>
  <div class="login-wrap">
    <div class="nav">Burc</div>
    <div class="container">
      <div class="form-outter">
        <h2>后台管理系统</h2>
        <el-form
          ref="loginFormRef"
          class="login-form"
          :model="loginFormData"
          :rules="rules"
          label-width="80px"
          :size="formSize"
          status-icon
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="loginFormData.username" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="loginFormData.password" type="password" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm(loginFormRef)">登录</el-button>
            <el-button @click="resetForm(loginFormRef)">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import type { FormInstance, FormRules } from 'element-plus'
import api from '@/apis/index'
import { useUserStore } from '@/stores'
import { setToken } from '@/utils/auth'

type sizeType = 'default' | 'small' | 'large'

const userStore = useUserStore()
// 返回 router 实例。相当于在模板中使用 $router。必须在 setup() 中调用
const router = useRouter()

const formSize = ref<sizeType>('default')
const loginFormRef = ref<FormInstance>()
const loginFormData = reactive({
  username: '',
  password: '',
})

const rules = reactive<FormRules>({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 10, message: '密码长度在6~10之间', trigger: 'blur' },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      api.user.loginMock(loginFormData)
      api.user.login(loginFormData).then((res: any) => {
        const { code, data } = res
        if (code === 200) {
          // 保存token
          setToken(data.token)
          userStore.setToken(data.token)
          // 跳转路由
          router.push('/goods')
        }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<style lang="less" scoped>
@navheight: 54px;
.login-wrap {
  width: 100vw;
  height: 100vh;
  .nav {
    height: @navheight;
    line-height: @navheight;
    padding-left: 22px;
    color: #fff;
    background-color: #000;
    backdrop-filter: blur(8px);
  }
  .container {
    position: relative;
    height: calc(100vh - @navheight);
    background: url('@/assets/images/login_bg.jpg');
    background-size: cover;
    overflow: hidden;
  }
  .form-outter {
    position: absolute;
    top: 40%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 550px;
    height: 260px;
    padding: 20px;
    border-radius: 12px;
    background-color: #fff;
    & h2 {
      font-size: 26px;
      font-weight: bold;
      text-align: center;
      padding: 10px 0 30px 0;
    }
  }
  .login-form {
    margin-top: 0px;
    :deep(.el-form-item__label) {
    }
  }
}
</style>
